package cn.ppool.test.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

    /**
     * 判断是否正确日期
     *
     * @param str
     * @param fomat
     * @return
     */
    public static boolean isDateTime(String str, String fomat) {
        SimpleDateFormat format = new SimpleDateFormat(fomat); // 指定日期格式为四位年/两位月份/两位日期，注意yyyy-MM-dd HH:mm:ss区分大小写；
        try {// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }

    /**
     * 字符串日期转换成date
     *
     * @param date
     * @return
     */
    public static Date StrToTime(String date) throws Exception {
        if (date.indexOf('.') > -1) {
            date = date.substring(0, date.indexOf('.'));
        }
        if (date.length() == "yyyy-MM-dd HH:mm:ss".length() && Integer.valueOf(date.substring(0, 2)) >= 19 && isDateTime(date, "yyyy-MM-dd HH:mm:ss")) {
            DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return format1.parse(date);
        } else if (date.length() == "yyyyMMddHHmmss".length() && Integer.valueOf(date.substring(0, 2)) >= 19 && isDateTime(date, "yyyyMMddHHmmss")) {
            DateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
            return format1.parse(date);
        } else if (date.length() == "yyyy-MM-dd".length() && Integer.valueOf(date.substring(0, 2)) >= 19 && isDateTime(date, "yyyy-MM-dd")) { //YYYY-MM-DD
            DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            return format1.parse(date);
        } else if (date.length() == "yyyyMMdd".length() && Integer.valueOf(date.substring(0, 2)) >= 19 && isDateTime(date, "yyyyMMdd")) { //YYYY-MM-DD
            DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
            return format1.parse(date);
        } else if (SuperUtil.isInt(date)) {//时间戳单位是秒，不是毫秒
            return new Date(Long.parseLong(date) * 1000);
        }
        throw new Exception("不支持的时间：" + date);
    }


    /**
     * 时间戳转时间
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static Date StrToTime(Long date) throws Exception {
        return new Date(date * 1000);
    }


    /**
     * 设置日期时间
     *
     * @param date
     * @param Y
     * @param M
     * @param D
     * @param H
     * @param Min
     * @param S
     * @return
     */
    public static Date SetDateTime(Date date, Integer Y, Integer M, Integer D, Integer H, Integer Min, Integer S) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, Y);
        calendar.set(Calendar.MONTH, (M - 1));
        calendar.set(Calendar.DATE, D);
        calendar.set(Calendar.HOUR_OF_DAY, H);
        calendar.set(Calendar.MINUTE, Min);
        calendar.set(Calendar.SECOND, S);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


}
